<?php
namespace app\manager\controller;

use think\Db;
use app\common\traits\Provide;

class User extends Base
{
    use Provide;

    public function userList()
    {
        $search = input('get.');
        if(request()->isAjax() || isset($search['excel']))
        {
            $query = Db::name('user')
                ->alias('u')
                ->join('role r','r.id = u.role')
                ->field('u.id,u.name,u.phone,u.avatar, r.name as role_name,u.state,u.online_time,u.c_time');
            if(!empty($search['search_name']))
            {
                $query->where('u.name|u.phone', $search['search_name']);
            }
            $count = $query->count();

            if(isset($search['page']) && isset($search['limit']))
            {
                $query->page($search['page'], $search['limit']);
            }
            $list = $query->order('u.id desc')->select();

            if(isset($search['excel']))
            {
                $name  = '用户列表';
                $title = ['ID', '用户名', '手机号', '头像', '角色','状态', '在线时长','添加时间'];
                exportCsv($name, $title, $list);
            }else{
                return responseJson(0,'success', $list, $count);
            }
        }else{
            return $this->fetch('user_list');
        }
    }

    /**
     * 添加用户
     */
    public function addUser()
    {
        if(request()->isAjax())
        {
            $data = input('post.');
            $is_phone = Db::name('user')->where('phone', $data['phone'])->value('id');
            if($is_phone)
            {
                return responseJson(400,'手机号已存在');
            }
            $is_name = Db::name('user')->where('name', $data['name'])->value('id');
            if($is_name)
            {
                return responseJson(400,'会员名已存在');
            }
            unset($data['file']);
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
            $data['c_time'] = time();
            $result = Db::name('user')->insert($data);
            if($result)
            {
                return responseJson(200,'添加成功');
            }else{
                return responseJson(400,'添加失败');
            }
        }else{
            $data['role'] = Db::name('role')->where('state',1)->where('id','<>','1')->select();
            $this->assign('data', $data);
            return $this->fetch('add_user');
        }
    }

    /*
     * 修改用户
     */
    public function editUser()
    {
        $id = input('id');
        if(request()->isAjax())
        {
            $data = input('post.');
            $is_phone = Db::name('user')->where('phone', $data['phone'])->where('id', '<>', $id)->value('id');
            if($is_phone)
            {
                return responseJson(400,'手机号已存在');
            }
            $is_name = Db::name('user')->where('name', $data['name'])->where('id', '<>', $id)->value('id');
            if($is_name)
            {
                return responseJson(400,'会员名已存在');
            }
            if(input('post.password'))
            {
                $data['password'] = password_hash(input('post.password'), PASSWORD_DEFAULT);
            }
            unset($data['file']);
            $data['u_time'] = time();
            $result = Db::name('user')->where('id', $id)->update($data);
            if($result)
            {
                return responseJson(200,'修改成功');
            }else{
                return responseJson(400,'修改失败');
            }
        }else{
            $data['role'] = Db::name('role')->where('state',1)->select();
            $data['info'] = Db::name('user')->where('id', $id)->find();
            $this->assign('data', $data);
            return $this->fetch('edit_user');
        }
    }

    /*
     * 删除
     */
    public function delUser()
    {
        $id = input('uid');
        $result = Db::name('user')->where('id', $id)->delete();
        if(!$result)
        {
            return responseJson(400,'删除失败');
        }
        return responseJson(200,'修改成功');
    }

    /*
     * 角色
     */
    public function roleList()
    {
        if(request()->isAjax())
        {
            $list  = Db::name('role')->select();
            $count = Db::name('role')->count('id');
            return responseJson(0,'success', $list, $count);
        }else{
            return $this->fetch('role_list');
        }
    }

    public function addRole()
    {
        if(request()->isAjax())
        {
            $data = input('post.');
            unset($data['file']);
            $result = Db::name('role')->insert($data);
            if($result)
            {
                return responseJson(200,'添加成功');
            }else{
                return responseJson(400,'添加失败');
            }
        }else{
            return $this->fetch('add_role');
        }
    }
}